package flyweight

const (
	UnitColorBlack = iota + 1
	UnitColorWhite
)

var units = map[uint]*goPieceUnit{
	UnitColorBlack: {
		Color:  "黑色",
		Radius: 1,
	},
	UnitColorWhite: {
		Color:  "白色",
		Radius: 1,
	},
}

// goPieceUnit 棋子享元
type goPieceUnit struct {
	Color  string
	Radius uint
}

// NewGoPieceUnit 工厂
func NewGoPieceUnit(color uint) *goPieceUnit {
	return units[color]
}

// goPiece 棋子
type goPiece struct {
	Unit     *goPieceUnit
	Id, X, Y uint
}

// GoBoard 棋局
type GoBoard struct {
	GoPieces map[uint]*goPiece
}

// NewGoBoard 初始化棋局
func NewGoBoard() *GoBoard {
	board := &GoBoard{GoPieces: map[uint]*goPiece{}}
	var i uint
	for ; i < 180; i++ {
		board.GoPieces[i] = &goPiece{
			Unit: NewGoPieceUnit(UnitColorBlack),
			Id:   i,
			X:    0,
			Y:    0,
		}
	}
	for ; i < 360; i++ {
		board.GoPieces[i] = &goPiece{
			Unit: NewGoPieceUnit(UnitColorWhite),
			Id:   i,
			X:    0,
			Y:    0,
		}
	}
	return board
}

// Move 移动棋子
func (c *GoBoard) Move(id, x, y uint) {
	c.GoPieces[id].X = x
	c.GoPieces[id].Y = y
}
